﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Objects;
using Simply.DataTier.Repository.Interface;
using Simply.DataTier.Persistance;
using Simply.DataTier.Repository.Implementation;
using Domain;
using Domain.Type;

namespace Simply.DataTier.Repository.Implementation
{
    public class StaticDataEFRepository : BaseEFRepository, IStaticDataRepository
    {

        #region Cache 
        
        #endregion

        
        #region IStaticDataRepository Members

        public List<IndustryDO> GetAllIndustries()
        {
            using (SimplyDataContext context = new SimplyDataContext())
            {
                context.ObjectTrackingEnabled = false;
                List<IndustryDO> industries = (from ind in context.Industries
                                               select new IndustryDO()
                                               {
                                                   Id = ind.IndustryId,
                                                   NameEn = ind.NameEn,
                                                   NameRu = ind.NameRu,
                                                   NameAm = ind.NameAm,
                                               }).ToList();
                return industries;
            }
        }

        public List<StyleDO> GetAllStyles()
        {
            using (SimplyDataContext context = new SimplyDataContext())
            {
                context.ObjectTrackingEnabled = false;
                List<StyleDO> styles = (from stl in context.Styles
                                        select new StyleDO()
                                        {
                                            Id = stl.StyleId,
                                            NameEn = stl.NameEn,
                                            NameRu = stl.NameRu,
                                            NameAm = stl.NameAm,
                                        }).ToList();
                return styles;
            }
        }

        public List<ColorThemeDO> GetAllColorThemes()
        {
            using (SimplyDataContext context = new SimplyDataContext())
            {
                context.ObjectTrackingEnabled = false;
                List<ColorThemeDO> colorThemes = (from col in context.ColorThemes
                                                  select new ColorThemeDO()
                                                    {
                                                        Id = col.ColorThemeId,
                                                        NameEn = col.NameEn,
                                                        NameRu = col.NameRu,
                                                        NameAm = col.NameAm,
                                                    }).ToList();
                return colorThemes;
            }
        }

        public List<TagDO> GetAllTags()
        {
            using (SimplyDataContext context = new SimplyDataContext())
            {
                context.ObjectTrackingEnabled = false;
                List<TagDO> tags = (from tag in context.Tags
                                                  select new TagDO()
                                                  {
                                                      Id = tag.TagId,
                                                      NameEn = tag.NameEn,
                                                      NameRu = tag.NameRu,
                                                      NameAm = tag.NameAm,
                                                  }).ToList();
                return tags;
            }
        }

        public List<PrintMediaTypeDO> GetAllPrintMediaTypes()
        {
            using (var ctx = new SimplyDataContext())
            {
                ctx.ObjectTrackingEnabled = false;
                List<PrintMediaTypeDO> res = (from t in ctx.PrintMediaTypes
                                              select new PrintMediaTypeDO()
                                    {
                                        Code = t.MediaTypeCode,
                                        NameEn = t.NameEn,
                                        NameRu = t.NameRu,
                                        NameAm = t.NameAm,
                                        DescriptionAm = t.DescriptionAm,
                                        DescriptionEn = t.DescriptionEn,
                                        DescriptionRu = t.DescriptionRu,
                                    }).ToList();
                return res;
            }
        }
        public List<DesignGroupDO> GetAllDesignGroups()
        {
            using (var ctx = new SimplyDataContext())
            {
                ctx.ObjectTrackingEnabled = false;
                List<DesignGroupDO> res = (from t in ctx.DesignGroups
                                              select new DesignGroupDO()
                                              {
                                                  Id = t.DesignGroupId,
                                                  NameEn = t.NameEn,
                                                  NameRu = t.NameRu,
                                                  NameAm = t.NameAm,
                                              }).ToList();
                return res;
            }
        }

        public List<FontDO> GetAllFonts()
        {
            using (var ctx = new SimplyDataContext())
            {
                ctx.ObjectTrackingEnabled = false;
                List<FontDO> res = (from t in ctx.Fonts
                                           select new FontDO()
                                           {
                                               Id = t.FontId,
                                               FileName = t.FileName,
                                               CreateDate = t.CreateDate,
                                               IsUnicode = t.IsUnicode,
                                               LanguageCode = t.LanguageCode,
                                               Name = t.Name,
                                               Style = t.Style
                                           }).ToList();
                return res;
            }
        }

        public int GetProductCountForStyle(int styleId, string productType)
        {
            using (SimplyDataContext context = new SimplyDataContext())
            {
                context.ObjectTrackingEnabled = false;
                return context.Products.Count(m => m.ProductCode.Equals(productType) && m.StyleId == styleId);
            }
        }

        public int GetProductCountForIndustry(int industryId, string productType)
        {
            using (SimplyDataContext context = new SimplyDataContext())
            {
                context.ObjectTrackingEnabled = false;
                return context.Products.Count(m => m.ProductCode.Equals(productType) && m.IndustryId == industryId);
            }
        }

        public int GetProductCountForColor(int colorId, string productType)
        {
            using (SimplyDataContext context = new SimplyDataContext())
            {
                context.ObjectTrackingEnabled = false;
                return context.Products.Count(m => m.ProductCode.Equals(productType) && m.ColorThemeId == colorId);
            }
        }

        public FontDO GetFontById(int fontId)
        {
            using(var context = new SimplyDataContext())
            {
                context.ObjectTrackingEnabled = false;
                FontDO fontDo = (from f in context.Fonts
                                 where f.FontId == fontId
                                 select new FontDO()
                                 {
                                     Id = f.FontId,
                                     FileName = f.FileName,
                                     IsUnicode = f.IsUnicode,
                                     LanguageCode = f.Language.Code,
                                     Name = f.Name,
                                     Style = f.Style
                                 }).Single();
                return fontDo;
            }
        }

        public StyleDO GetStyleById(int styleId)
        {
            using (var ctx = new SimplyDataContext())
            {
                ctx.ObjectTrackingEnabled = false;
                StyleDO styleDo = (from s in ctx.Styles
                                  where s.StyleId == styleId
                                 select new StyleDO()
                                 {
                                     Id = s.StyleId,
                                     NameEn = s.NameEn,
                                     NameRu = s.NameRu,
                                     NameAm = s.NameAm,
                                 }).Single();
                return styleDo;
            }
        }


        public IndustryDO GetIndustryById(int industryId)
        {
            using (var ctx = new SimplyDataContext())
            {
                ctx.ObjectTrackingEnabled = false;
                IndustryDO industryDo = (from s in ctx.Industries
                                   where s.IndustryId == industryId
                                   select new IndustryDO()
                                   {
                                       Id = s.IndustryId,
                                       NameEn = s.NameEn,
                                       NameRu = s.NameRu,
                                       NameAm = s.NameAm,
                                   }).Single();
                return industryDo;
            }
        }

        public List<PrintMethodDO> GetPrintMethodsByMediaType(string mediaType)
        {
            using (SimplyDataContext context = new SimplyDataContext())
            {
                List<PrintMethodDO> printMethods = (from m in context.PrintMethods
                                                    from t in m.PrintMediaTypeToPrintMethods
                                                    where t.MediaTypeCode.Equals(mediaType)
                                                    select new PrintMethodDO()
                                                    {
                                                        Code = m.MethodCode,
                                                        BothSide = m.BothSide,
                                                        DescriptionAm = m.DescriptionAm,
                                                        DescriptionEn = m.DescriptionEn,
                                                        DescriptionRu = m.DescriptionRu,
                                                        NameEn = m.NameEn,
                                                        NameRu = m.NameRu,
                                                        NameAm = m.NameAm,
                                                        Duration = m.Duration,
                                                        AvailableFrom = m.AvailableFrom
                                                    }).ToList();
                return printMethods;
            }
        }

        public PrintMediaDO GetPrintMediaByCode(string mediaCode)
        {
            using (SimplyDataContext context = new SimplyDataContext())
            {
                PrintMediaDO media = (from p in context.PrintMedias
                                      where p.MediaCode.Equals(mediaCode)
                                      select new PrintMediaDO()
                                      {
                                          Code = p.MediaCode,
                                          DefaultPrintMethod = new PrintMethodDO() { Code = p.DefaultPrintMethodCode },
                                          DescriptionAm = p.DescriptionAm,
                                          DescriptionEn = p.DescriptionEn,
                                          DescriptionRu = p.DescriptionRu,
                                          NameEn = p.NameEn,
                                          NameRu = p.NameRu,
                                          NameAm = p.NameAm,
                                          Name = p.Name,
                                          
                                          Type = new PrintMediaTypeDO() { Code = p.MediaTypeCode },
                                          Weight = p.WeightGr.HasValue ? p.WeightGr.Value : 0,
                                          AvailableFrom = p.AvailableFrom
                                      }).Single();
                return media;
            }
        }


        public int CreateStyle(StyleDO obj)
        {
            using (var ctx = new SimplyDataContext())
            {
                var entity = new Style()
                {
                    NameAm = obj.NameAm,
                    NameEn = obj.NameEn,
                    NameRu = obj.NameRu,
                };

                ctx.Styles.InsertOnSubmit(entity);
                ctx.SubmitChanges();

                return entity.StyleId;
            }
        }
        public int CreateIndustry(IndustryDO obj)
        {
            using (var ctx = new SimplyDataContext())
            {
                var entity = new Industry()
                {
                    NameAm = obj.NameAm,
                    NameEn = obj.NameEn,
                    NameRu = obj.NameRu,
                };

                ctx.Industries.InsertOnSubmit(entity);
                ctx.SubmitChanges();

                return entity.IndustryId;
            }
        }
        public int CreateColorTheme(ColorThemeDO obj)
        {
            using (var ctx = new SimplyDataContext())
            {
                var entity = new ColorTheme()
                {
                    NameAm = obj.NameAm,
                    NameEn = obj.NameEn,
                    NameRu = obj.NameRu,
                    RGBHex = obj.RGBHex
                };

                ctx.ColorThemes.InsertOnSubmit(entity);
                ctx.SubmitChanges();

                return entity.ColorThemeId;
            }
        }
        public long CreateTag(TagDO obj)
        {
            using (var ctx = new SimplyDataContext())
            {
                var entity = new Tag()
                {
                    NameAm = obj.NameAm,
                    NameEn = obj.NameEn,
                    NameRu = obj.NameRu,
                };

                ctx.Tags.InsertOnSubmit(entity);
                ctx.SubmitChanges();

                return entity.TagId;
            }
        }

        public void CreatePrintMediaType(PrintMediaTypeDO obj)
        {
            using (var ctx = new SimplyDataContext())
            {
                var entity = new PrintMediaType()
                {
                    NameAm = obj.NameAm,
                    NameEn = obj.NameEn,
                    NameRu = obj.NameRu,
                    CreateDate = DateTime.Now,
                    DescriptionAm = obj.DescriptionAm,
                    DescriptionEn = obj.DescriptionEn,
                    DescriptionRu = obj.DescriptionRu,
                    MediaTypeCode = obj.Code,
                    
                };

                ctx.PrintMediaTypes.InsertOnSubmit(entity);
                ctx.SubmitChanges();
            }
        }

        public long CreateDesignGroup(DesignGroupDO obj)
        {
            using (var ctx = new SimplyDataContext())
            {
                var entity = new DesignGroup()
                {
                    NameAm = obj.NameAm,
                    NameEn = obj.NameEn,
                    NameRu = obj.NameRu,
                };

                ctx.DesignGroups.InsertOnSubmit(entity);
                ctx.SubmitChanges();

                return entity.DesignGroupId;
            }
        }

        public int CreateFont(FontDO font)
        {
            using (var ctx = new SimplyDataContext())
            {
                var entity = new Font()
                {
                    Name = font.Name,
                    LanguageCode = font.LanguageCode,
                    IsUnicode = font.IsUnicode,
                    FileName = font.FileName,
                    CreateDate = DateTime.Now,
                    Style = font.Style
                };

                ctx.Fonts.InsertOnSubmit(entity);
                ctx.SubmitChanges();

                return entity.FontId;
            }
        }
        #endregion
    }
}
